﻿// Author: Iulian Lita
// Project: UIModule
// Path: C:\Users\Tym\Documents\Visual Studio 2008\Projects\Library\UIModule
// Creation date: 1/13/2009 2:42 PM
// Last modified: 1/14/2009 7:32 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UsersModule.UserActions;

namespace ProjectIp
{
    public partial class UsersForms : Form
    {
        private Form _parentForm;
        private DefaultUserActions _userActions;

        public Form ParentForm
        {
            get { return _parentForm; }
            set { _parentForm = value; }
        }
    
      
        public UsersForms(DefaultUserActions userActions)
        {
            _userActions = userActions;
            InitializeComponent();
        }

        private void UsersForms_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'allFieldsLibrary.detaliiProdus' table. You can move, or remove it, as needed.
            allFieldsLibrary.EnforceConstraints = false;
            this.detaliiProdusTableAdapter.GetAvailableItems(this.allFieldsLibrary.detaliiProdus);
           
        }

        private void borrow_btn_Click(object sender, EventArgs e)
        {
            try
            {
                int idProdus = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
                if (_userActions.LoanItem(idProdus))
                {
                    MessageBox.Show("Loan OK");
                    this.detaliiProdusTableAdapter.GetAvailableItems(this.allFieldsLibrary.detaliiProdus);
                }
                else
                    MessageBox.Show("Loan not OK");
            }
            catch (Exception exc)
            {
                Error.ErrorManagement.ManageException(exc);
            }
        }

        private void exit_btn_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void logout_btn_Click(object sender, EventArgs e)
        {
            ParentForm.Show();
            this.Dispose();
        }

        private void go_btn_Click(object sender, EventArgs e)
        {
            try
            {
                if (uSearchBy_cbox.SelectedIndex == 0)
                    detaliiProdusTableAdapter.SelectByTitle(allFieldsLibrary.detaliiProdus, uSearch_tbox.Text);
                else
                    detaliiProdusTableAdapter.SelectByAuthor(allFieldsLibrary.detaliiProdus, uSearch_tbox.Text);
            }
            catch (Exception exc)
            {
                MessageBox.Show("Unable to search in database!!");
                Error.ErrorManagement.ManageException(exc);
            }
        }

        private void recommend_btn_Click(object sender, EventArgs e)
        {
            try
            {
                int itemIndex = (int)dataGridViewRecommend.SelectedRows[0].Cells[0].Value;
                if (!_userActions.RecommendItem(itemIndex))
                    MessageBox.Show("Recommedation failed, make sure you're connected as a student or a teacher");
                else
                    MessageBox.Show("Recommandation succeded");

                
            }
            catch (Exception exc)
            {
                Error.ErrorManagement.ManageException(exc);
                MessageBox.Show("Nu am reusit sa trimit recomandarea!!");
            }
        }

        private void tabControl1_TabIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                this.detaliiProdusTableAdapter.GetAvailableItems(this.allFieldsLibrary.detaliiProdus);
            }
            else
            {
                this.detaliiProdusTableAdapter.GetUnavailableItems(this.allFieldsLibrary.detaliiProdus);
            }
        }

        private void recommend_tabPage_Click(object sender, EventArgs e)
        {

        }

        private void btnSee_Click(object sender, EventArgs e)
        {
            this.detaliiProdusTableAdapter.GetUnavailableItems(this.allFieldsLibrary.detaliiProdus);
        }
    }
}
